<html>
<head><meta charset="utf-8"><title>Moving Ty to be closer to chalk&#x27;s TyKind · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Moving.20Ty.20to.20be.20closer.20to.20chalk&#x27;s.20TyKind.html">Moving Ty to be closer to chalk&#x27;s TyKind</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="228161742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Moving%20Ty%20to%20be%20closer%20to%20chalk%27s%20TyKind/near/228161742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Moving.20Ty.20to.20be.20closer.20to.20chalk&#x27;s.20TyKind.html#228161742">(Feb 28 2021 at 22:30)</a>:</h4>
<p>I've been looking at our <code>Ty::Infer(InferTy)</code> variant to bring it closer to chalks representation <code>InferenceVar(InferenceVar, TyVariableKind)</code> and noticed that our InferTy basically has 4 variable kinds unlike chalk which has 3. We have an extra <code>MaybeNever</code> variant:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">enum</span> <span class="nc">InferTy</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">TypeVar</span><span class="p">(</span><span class="n">unify</span>::<span class="n">TypeVarId</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">IntVar</span><span class="p">(</span><span class="n">unify</span>::<span class="n">TypeVarId</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">FloatVar</span><span class="p">(</span><span class="n">unify</span>::<span class="n">TypeVarId</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">MaybeNeverTypeVar</span><span class="p">(</span><span class="n">unify</span>::<span class="n">TypeVarId</span><span class="p">),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p><a href="https://github.com/rust-analyzer/rust-analyzer/blob/23d7dbfa5e7ba2cebf8c3f79b5d31285d79c1527/crates/hir_ty/src/infer.rs#L658-L663">crates/hir_ty/src/infer.rs#L658-L663</a><br>
If we were to switch over to chalk's typesystem one day we would lose this information if I'm not mistaken right? Is this something that should actually go into chalk?</p>



<a name="228162429"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Moving%20Ty%20to%20be%20closer%20to%20chalk%27s%20TyKind/near/228162429" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Moving.20Ty.20to.20be.20closer.20to.20chalk&#x27;s.20TyKind.html#228162429">(Feb 28 2021 at 22:43)</a>:</h4>
<p>Chalk does support the never type, and rust's <code>InferTy</code> doesn't contain a <code>Never</code> variant, so I don't think a variant needs to be added in chalk</p>



<a name="228162528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Moving%20Ty%20to%20be%20closer%20to%20chalk%27s%20TyKind/near/228162528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Moving.20Ty.20to.20be.20closer.20to.20chalk&#x27;s.20TyKind.html#228162528">(Feb 28 2021 at 22:44)</a>:</h4>
<p>Unless <code>MaybeNever</code> is something RA added for better IDE support?</p>



<a name="228162664"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Moving%20Ty%20to%20be%20closer%20to%20chalk%27s%20TyKind/near/228162664" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Moving.20Ty.20to.20be.20closer.20to.20chalk&#x27;s.20TyKind.html#228162664">(Feb 28 2021 at 22:46)</a>:</h4>
<p>From what I've seen it's used to specify type inference fallback to use never instead of the unit type in RA</p>



<a name="228162763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Moving%20Ty%20to%20be%20closer%20to%20chalk%27s%20TyKind/near/228162763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> detrumi <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Moving.20Ty.20to.20be.20closer.20to.20chalk&#x27;s.20TyKind.html#228162763">(Feb 28 2021 at 22:48)</a>:</h4>
<p>Chalk and rustc use a <code>TyKind::Never</code> type instead, but yeah then you wouldn't have a <code>TypeVarId</code>. But I don't think that's actually needed anywhere</p>



<a name="228164261"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Moving%20Ty%20to%20be%20closer%20to%20chalk%27s%20TyKind/near/228164261" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Moving.20Ty.20to.20be.20closer.20to.20chalk&#x27;s.20TyKind.html#228164261">(Feb 28 2021 at 23:16)</a>:</h4>
<p>I see, figured it's gonna be like that. I imagine the problem lies in how type inference is structured in RA then, Florian should know. I haven't looked too much into how type inference in RA works(or rather that much in general either).</p>



<a name="228195023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Moving%20Ty%20to%20be%20closer%20to%20chalk%27s%20TyKind/near/228195023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Moving.20Ty.20to.20be.20closer.20to.20chalk&#x27;s.20TyKind.html#228195023">(Mar 01 2021 at 08:25)</a>:</h4>
<p>I'm pretty sure rustc has a similar mechanism? let me check</p>



<a name="228195513"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Moving%20Ty%20to%20be%20closer%20to%20chalk%27s%20TyKind/near/228195513" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Moving.20Ty.20to.20be.20closer.20to.20chalk&#x27;s.20TyKind.html#228195513">(Mar 01 2021 at 08:31)</a>:</h4>
<p>ah, rustc does this with a flag in a separate data structure indicating that the type variable is diverging</p>



<a name="228195581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Moving%20Ty%20to%20be%20closer%20to%20chalk%27s%20TyKind/near/228195581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Moving.20Ty.20to.20be.20closer.20to.20chalk&#x27;s.20TyKind.html#228195581">(Mar 01 2021 at 08:31)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/05c300144cebbf3ddaff213b7485f669a9e0b660/compiler/rustc_infer/src/infer/type_variable.rs#L122">https://github.com/rust-lang/rust/blob/05c300144cebbf3ddaff213b7485f669a9e0b660/compiler/rustc_infer/src/infer/type_variable.rs#L122</a></p>



<a name="228195666"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Moving%20Ty%20to%20be%20closer%20to%20chalk%27s%20TyKind/near/228195666" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Moving.20Ty.20to.20be.20closer.20to.20chalk&#x27;s.20TyKind.html#228195666">(Mar 01 2021 at 08:32)</a>:</h4>
<p>that would probably be a good refactoring, and we'll want to keep additional information on type variables (like the origin) anyway</p>



<a name="228207540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Moving%20Ty%20to%20be%20closer%20to%20chalk%27s%20TyKind/near/228207540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Moving.20Ty.20to.20be.20closer.20to.20chalk&#x27;s.20TyKind.html#228207540">(Mar 01 2021 at 10:27)</a>:</h4>
<p>Ah I see, we would store this in the InferenceTable then if i see this right?</p>



<a name="228208168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Moving%20Ty%20to%20be%20closer%20to%20chalk%27s%20TyKind/near/228208168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Moving.20Ty.20to.20be.20closer.20to.20chalk&#x27;s.20TyKind.html#228208168">(Mar 01 2021 at 10:32)</a>:</h4>
<p>I think it needs to be a separate table (keyed by variable ID)</p>



<a name="228208553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Moving%20Ty%20to%20be%20closer%20to%20chalk%27s%20TyKind/near/228208553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Moving.20Ty.20to.20be.20closer.20to.20chalk&#x27;s.20TyKind.html#228208553">(Mar 01 2021 at 10:35)</a>:</h4>
<p>sorrry, meant it as in whether the extra table should be in InferenceTable, since InferenceTable needs it for the fallback.</p>



<a name="228208576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Moving%20Ty%20to%20be%20closer%20to%20chalk%27s%20TyKind/near/228208576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Moving.20Ty.20to.20be.20closer.20to.20chalk&#x27;s.20TyKind.html#228208576">(Mar 01 2021 at 10:35)</a>:</h4>
<p>or pass it by ref on each call where required</p>



<a name="228210535"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Moving%20Ty%20to%20be%20closer%20to%20chalk%27s%20TyKind/near/228210535" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Moving.20Ty.20to.20be.20closer.20to.20chalk&#x27;s.20TyKind.html#228210535">(Mar 01 2021 at 10:51)</a>:</h4>
<p>aah right sorry, yeah it makes sense to put it there</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>